/**
 * 详情对话框
 */
var CategoryInfoDlg = {
    data: {
        categoryName: "",
        icon: "",
        weights: "",
        level: "",
        parentId: "",
        revision: "",
        createTime: "",
        updateTime: "",
        recordStatus: ""
    },
    prev: {
        pName: "",
        parentId: ""
    }
};

layui.use(['form', 'admin', 'ax','simTool'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var simTool = layui.simTool;

    //让当前iframe弹层高度适应
    admin.iframeAuto();


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/category/addItem", function (data) {
            Feng.success("添加成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });
    //图标上传
    var sim=new simTool("/upload","#iconPicBox")
    sim.uploadImg()




    // 点击父级菜单
    $('#pName').click(function () {
        var formName = encodeURIComponent("parent.CategoryInfoDlg.prev.pName");
        var formId = encodeURIComponent("parent.CategoryInfoDlg.prev.parentId");
        var treeUrl = encodeURIComponent("/category/selectCategoryTreeList");
        layer.open({
            type: 2,
            title: '父级菜单',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#parentId").val(CategoryInfoDlg.prev.parentId);
                $("#pName").val(CategoryInfoDlg.prev.pName);
                var parentId = CategoryInfoDlg.prev.parentId;
                if(parentId =="" || parseInt(parentId)<=0){
                    $("#homeCategory").css("display","block");
                }else{
                    $("#homeCategory").css("display","none");
                    $("#isImportant").removeAttr("checked");
                }
            }
        });
    });

    /**
     * 长度限制
     */
    form.verify({
        limitLength : function(value, item){
            //value：表单的值、item：表单的DOM对象
            /*if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                return '用户名不能有特殊字符';
            }*/
            if(/(^\_)|(\__)|(\_+$)/.test(value)){
                return '类型首尾不能出现下划线\'_\'';
            }
            if(/^\d+\d+\d$/.test(value)){
                return '类型不能全为数字';
            }
            if(value.length>5){
                return '分类字数限制为5个';
            }
        }

        //我们既支持上述函数式的方式，也支持下述数组的形式
        //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
        ,pass: [
            /^[\S]{6,12}$/
            ,'密码必须6到12位，且不能出现空格'
        ]
    });
});